const express = require('express')
const path = require('path');
const app = express();
app.use(express.static(path.join(__dirname, 'public')));

const log4js = require('log4js')
log4js.configure({
  appenders: {
    file: {
      type: 'file',
      filename: 'app.log',
      layout: {
        type: 'pattern',
        pattern: '%r %p - %m'
      }
    }
  },
  categories: {
    default: {
      appenders: ['file'],
      level: 'debug'
    }
  }
})
const logger = log4js.getLogger();


const http = require('http');
const http_server = http.createServer(app);


//https://www.npmjs.com/package/socket.io
const socketIO = require('socket.io')
const io = socketIO(http_server); //Attaches socket.io to a server or port.

const USER_MAX_COUNT = 2;

//bind socket.io with http_server
io.sockets.on('connection', (socket) => {
  socket.on('message', (roomId, data)=>{
    console.log(`服务器接收到信息:${data},准备转发给房间:${roomId}里的所有人`);

    socket.to(roomId).emit('message', roomId, socket.id/*告诉对方是我发送的*/, data)//房间内所有人,除自己外
    //socket.in(roomId).emit('message', roomId, socket.id/*告诉对方是我发送的*/, data)//房间内所有人 // emmm 新版效果貌似和 .to 是一样的 https://socket.io/docs/v4/server-api/#socket-to-room
    socket.emit('message', roomId, socket.id, data)
  });

  socket.on('join', (roomId) => {
    socket.join(roomId); //如果是第一次 会帮你自动创建对应的room
    const room = io.sockets.adapter.rooms.get(roomId);
    /*
      io.sockets.adapter.rooms: Map(2) {
        'IXD-JS-A42tWuNDLAAAD' => Set(1) { 'IXD-JS-A42tWuNDLAAAD' },
        'faf' => Set(1) { 'IXD-JS-A42tWuNDLAAAD' }
      }
    * */
    // console.log(socket.rooms);// socket.rooms: Set(2) { 'IXD-JS-A42tWuNDLAAAD', 'faf' } , 第一个参数是socket.id, 第二个参数是 roomId
    const users_total_number = room.size;
    logger.log(`the number of user in room is ${users_total_number}`);

    socket.emit('joined', roomId, socket.id/*用户id*/);
    // socket.to(roomId).emit() //给房间内除自己之外
    // io.in(roomId).emit() //给目标房间的所有人
    //socket.broadcast.emit('joined',roomId,socket.id) //给当前节点除自己以外所有人

    if (users_total_number <= USER_MAX_COUNT) {
      socket.emit('joined', roomId, socket.id);

      if (users_total_number > 1) {
        socket.to(roomId).emit('otherjoin', roomId)
      }

    } else {
      socket.leave(room);
      socket.emit('full', room, socket.id); //已经满了
    }
  });

  socket.on('leave', (roomId) => {
    const room = io.sockets.adapter.rooms.get(roomId);
    const users_total_number = room.size;
    socket.leave(roomId);
    logger.log(`the number of user in room is ${users_total_number - 1}`);

    socket.to(room).emit('bye', room, socket.id)//房间内所有人,除自己外

    socket.emit('leaved', roomId, socket.id/*用户id*/);
    // socket.to(roomId).emit() //给房间内除自己之外
    // io.in(roomId).emit() //给目标房间的所有人
    //socket.broadcast.emit('joined',roomId,socket.id) //给当前节点除自己以外所有人
  });
});

http_server.listen(9191);


/*const https = require('https');
const fs = require('fs');
const options = {
  key: fs.readFileSync('./cert/xxx.xxx.xxx.key'),
  cert: fs.readFileSync('./cert/xxx.xxx.xxx.pem')
}
https_server = https.createServer(options,app);
https_server.listen(443)*/
